package com.example.mc.handler.impl;

import com.example.common.base.constant.MqQueueConstant;
import com.example.common.i18n.util.AssertUtils;
import com.example.common.msg.dto.MemberLogDTO;
import com.example.common.msg.template.MsgTemplate;
import com.example.mc.handler.MessageHandler;
import com.example.mc.modules.mc.service.IMemberLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component(MqQueueConstant.MEMBER_LOG)
public class MemberLogMessageHander extends MessageHandler<MemberLogDTO> {

    @Autowired
    private IMemberLogService memberLogService;

    @Override
    public void check(MsgTemplate<MemberLogDTO> msgTemplate) {
        MemberLogDTO memberLogDTO = msgTemplate.getData();
        AssertUtils.isBlank(memberLogDTO.getTitle(),"请求内容不能为空");
    }

    @Override
    public boolean process(MsgTemplate<MemberLogDTO> msgTemplate) {
        try {
            //保存日志
            memberLogService.insertLog(msgTemplate.getData());
        } catch (Exception e) {
            log.error("日志保存异常");
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public void fail(MsgTemplate<MemberLogDTO> msgTemplate) {
        MemberLogDTO memberLog  = msgTemplate.getData();
        log.error("日志保存失败 -> 日志标题：{} -> 用户ID：{}", memberLog.getTitle(), memberLog.getCreateBy());
    }
}
